{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "URL: http://matplotlib.org/examples/pylab_examples/quiver_demo.html\n",
    "\n",
    "Most examples work across multiple plotting backends, this example is also available for:\n",
    "\n",
    "* [Matplotlib - quiver_demo](../matplotlib/quiver_demo.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "from holoviews import opts\n",
    "hv.extension('bokeh')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xs, ys = np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2)\n",
    "X, Y = np.meshgrid(xs, ys)\n",
    "U = np.cos(X)\n",
    "V = np.sin(Y)\n",
    "\n",
    "# Convert to magnitude and angle\n",
    "mag = np.sqrt(U**2 + V**2)\n",
    "angle = (np.pi/2.) - np.arctan2(U/mag, V/mag)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "label = 'Arrows scale with plot width, not view'\n",
    "\n",
    "opts.defaults(opts.VectorField(height=400, width=500, padding=0.1))\n",
    "\n",
    "vectorfield = hv.VectorField((xs, ys, angle, mag))\n",
    "vectorfield.relabel(label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "label = \"pivot='mid'; every third arrow\"\n",
    "\n",
    "vf_mid = hv.VectorField((xs[::3], ys[::3], angle[::3, ::3], mag[::3, ::3], ))\n",
    "points = hv.Points((X[::3, ::3].flat, Y[::3, ::3].flat))\n",
    "\n",
    "opts.defaults(opts.Points(color='red'))\n",
    "\n",
    "(vf_mid * points).relabel(label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "label = \"pivot='tip'; scales with x view\"\n",
    "\n",
    "vectorfield = hv.VectorField((xs, ys, angle, mag))\n",
    "points = hv.Points((X.flat, Y.flat))\n",
    "\n",
    "(points * vectorfield).opts(\n",
    "    opts.VectorField(magnitude='Magnitude', color='Magnitude',\n",
    "                     pivot='tip', line_width=2, title=label))"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
